// 1.写代码找出数组中最大值，最小值。如果参数isMax设置为true，返回最大值，否则返回最小值。

#include <iostream>
using namespace std;

int findMaxOrMin(const int arr[],int size , bool isMax){
    if (size <= 0)  
    {
        cerr << "错误，数组大小必须大于零" << endl;
        return 0;
    }

    int result = arr[0];

    for (int i = 1; i < size; i++)
    {
        if(isMax)
        {
            if (arr[i]>result)  
            {
                result = arr[i];
            }
            
        }
        else{
            if (arr[i] < result)
            {
                result = arr[i];
            }
            
        }
    }
    return result;
    
}

int main(int argc, char const *argv[])
{
    int arr[] = {3, 1, 4, 1, 5, 9, 2, 6};
    int size = sizeof(arr)/ sizeof(arr[0]);

    cout<<"数组:";
    for (int i = 0; i < size; i++)
    {
        cout << arr[i] << " ";

    }

    cout << endl;

    cout << "最大值: " <<findMaxOrMin(arr , size , true) << endl;
    cout << "最小值: " <<findMaxOrMin(arr , size , false) << endl;
    

    return 0;
}


